Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CompositeEMAC and use it to rewrite STM32 Ethernet MAC driver #438

Merged
merged 47 commits into from
Mar 28, 2025

Conversation

multiplemonomials
Copy link
Collaborator

@multiplemonomials multiplemonomials commented Feb 18, 2025

Summary of changes

This PR introduces a new framework for writing Ethernet MAC drivers. The existing EMAC interface requires MAC drivers to implement quite a lot of the specifics of memory management, MAC address tracking, and DMA themselves, even though quite a bit of this logic is common to all MAC drivers. This has led to duplicated code, and quite often to half-assed code as well as chip vendors have struggled to conform to the (admittedly not very well defined) EMAC API.

CompositeEMAC should make this a thing of the past. It's a class which implements the EMAC API and delegates its duties to four subclasses:

  • A MAC peripheral driver
  • A Tx DMA ring driver
  • An Rx DMA ring driver
  • A PHY driver

The first three of those classes are provided by the chip-specific EMAC driver, while the fourth comes from a library of PHY drivers and is not MCU specific. But this way most of the tough logic can be done in the non-MCU-specific layer, and all that needs to be done on a per-MCU basis is writing the low level MAC driver and low level code to interact with Ethernet descriptors.

See detailed documentation here: https://github.com/mbed-ce/mbed-os/blob/dev/stm32-emac-rewrite/connectivity/drivers/emac/CompositeEMAC.md

Impact of changes

  • Ethernet support added for STM32H5
  • Significant Ethernet speed improvement for STM32H7: MAC no longer blocks while packets are being sent
  • Also, data cache no longer gets disabled when you use Ethernet on STM32H7. Apparently someone just made the driver globally disable the cache to fix a bug...
  • Zero-copy Ethernet support for STM32 devices, so small packets can be received with less wasted memory and large packets can be received with no copying
  • Generic PHY driver system allows any PHY supported by Mbed to be used with any MCU. Previously the PHY was hardcoded into the EMAC driver, so you had to use that exact model of PHY.

Migration actions required

If you have a custom target for an STM32 board with Ethernet support, you will need to update your stm32_eth_init.c. The following changes were made to these files in this PR:

  • void HAL_ETH_MspInit(ETH_HandleTypeDef *heth) renamed to void EthInitPinmappings(void)
  • void HAL_ETH_MspDeInit(ETH_HandleTypeDef *heth) renamed to void EthDeinitPinmappings()
  • Any and all DCACHE disabling removed. The ethernet driver now fully supports the data cache!
  • void stm32_eth_init_weak_symbol_helper() removed, no longer needed
  • New function added: PinName EthGetPhyResetPin(void). This should return the GPIO pin connected to the phy reset line. If there is none, then it should return NC and a software reset will be used instead.

Here's an example of the changes: https://github.com/mbed-ce/mbed-os/pull/438/files#diff-7902496d820b51eb5e98851cbbe5cfcb5b2b68ef7af6d9a8e2650d2ada37ab4c

Documentation


Pull request type

[] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[X] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[X] Tests / results supplied as part of this PR

Successfully ran EMAC and netsocket tests on STM32H563 and STM32F429ZI. Only failures are the nanostack ones (which are known broken at present).


@multiplemonomials multiplemonomials force-pushed the dev/emac-test-improvements branch from d17f6ab to 404fdd4 Compare February 20, 2025 03:42
Base automatically changed from dev/emac-test-improvements to master February 20, 2025 04:00
Make progress on driver

More progress, but I realized that I can't free packets from an ISR...

Driver building, now I can start testing...

Add CompositeEthMac

MAC boots and can send!  Rx not working yet though

Sending and receiving sort of working!

Clean up CTP code to use structs

Fix dumb cache and off by one issues with Rx

Fix a couple memory issues, making progress...

Fix double free when transmission uses more than 1 descriptor

Start adding multicast support

Making progress on multicast support

Multicast filter working!

Tests passing!

Improve mcast filter test
@multiplemonomials multiplemonomials changed the title [draft] Add CompositeEMAC and use it to rewrite STM32 Ethernet MAC driver Add CompositeEMAC and use it to rewrite STM32 Ethernet MAC driver Mar 17, 2025
- Broken check in Tx DMA meant it was always copying packets
- Tx DMA would assert fail from trying to free nullptr if an allocation failed during packet copy
- Rx DMA would not reset firstDescIdx after seeing an error descriptor, leading to an attempt to receive a 0 length packet about 20% of the time we ran the EMAC memory test
…disabling input memory, and errors out if the MAC was working when it shouldn't've been.
@multiplemonomials multiplemonomials merged commit 7d6a1fe into master Mar 28, 2025
52 checks passed
@multiplemonomials multiplemonomials deleted the dev/stm32-emac-rewrite branch March 28, 2025 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants